package com.example.controller;

import com.example.entity.CarEntity;
import com.example.exception.ServiceException;
import com.example.interceptor.CarsPage;
import com.example.interceptor.Page;
import com.example.service.CarService;
import com.example.service.CarServiceImpl;
import com.example.springs.SpringUtil;
import com.example.springs.Springs;
import com.example.util.AopUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@Api("CarController相关接口")
public class CarController {
    @Autowired
    private CarService service;
    @RequestMapping(value = "/addCar", method = RequestMethod.POST)
    @ApiOperation(value = "insertBott", notes = "insertBott")
    public void insertBott(@RequestBody CarEntity entity)//少字段可以，多字段不行
    //public void insertBott(BigDecimal price)//少字段可以，多字段不行
    {
        //如果是以json方式穿过来，则不能这样接收，除非是默认的www.urlE...什么的
        System.out.println(entity);
//        StringBuilder sb = new StringBuilder();
//        try(BufferedReader reader = Springs.getReq().getReader()) {
//        char[]buff = new char[1024];
//        int len;
//        while((len = reader.read(buff)) != -1) {
//            sb.append(buff,0, len);
//        }
//    }catch (IOException e) {
//        e.printStackTrace();
//    }
//        sb.toString();
//        System.out.println(sb);
//        System.out.println(Springs.getReq().getParameter("price"));
//        //可能在body区域，需要在getparameter之前使用
//        CarEntity entity = new CarEntity();
//        entity.setPrice(BigDecimal.valueOf(Double.valueOf(sb.toString())));
//        entity.setDate(new Date());
//        service.insertCar(entity);
    }
    @RequestMapping(value = "/getCar")
    public CarEntity getCar(@RequestParam int id,@RequestParam String name)
    {
        //service.getCar(id);
        //没查到数据也可以抛一个异常，给前台
        return  service.getCar(id);
    }
    @RequestMapping(value = "/deleteCar/{id}")
    public void deleteCar(@PathVariable("id") int id)
    {
        if (id ==10)
        {
            throw  ServiceException.createException("1111");
            //throw new ServiceException("111",null,null);
        }
        service.deleteCar(id);
    }
    @RequestMapping(value = "/testSpring")
    public void testSpring()
    {
        System.out.println(service.toString());
        System.out.println(SpringUtil.getBean("carServiceImpl"));
        System.out.println(Springs.getBean(CarServiceImpl.class).toString());
        //spring容器里面能获得mvc容器里面的bean，说明只有一个容器了，不然父容器是获得不了子容器bean的
        System.out.println(Springs.getBean(CarController.class));
        System.out.println(Springs.getBean(AopUtil.class));
    }
    //写个异步任务，线程池调用，分别统计两张表里面数据，分别打印然后相加。
    @RequestMapping(value = "/testExecutor")
    public Integer testExecutor()
    {
        return service.getCarAndBootNum();
    }
    @RequestMapping(value = "/getCars")
    public List<CarEntity> getCars()
    {
        return service.getCars();
    }

    @RequestMapping(value = "/getCarsByPrice",method = RequestMethod.GET)
    @ApiOperation(value = "根据价格分页查询car", notes = "查询数据库中某个的学生信息")
    public CarsPage<CarEntity> getCars(@RequestParam(value = "price") @ApiParam("价格")BigDecimal price,
                            @RequestParam(value = "page") @ApiParam("当前页")int page,
                            @RequestParam(value = "rows") @ApiParam("页码") int rows)
    {
        Page page1 = new Page(page -1,rows);
        return service.getCarsByName(price,page1);
    }

}
